package it.apiconsulting.sharem.dao;


import it.apiconsulting.sharem.model.Brano;
import it.apiconsulting.sharem.util.HibernateUtil;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;


public class BranoDao extends HibernateUtil {
	
	public Brano add(Brano brano) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(brano);
		session.getTransaction().commit();
		return brano;
	}
	public Brano delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Brano brano = (Brano) session.load(Brano.class, id);
		if(null != brano) {
			session.delete(brano);
		}
		session.getTransaction().commit();
		return brano;
	}

	public List<Brano> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Brano> brani = null;
		try {
			
			Query query = session.createQuery("from brano");
			brani = (List<Brano>)query.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return brani;
	}
}
